﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using System.IO;
using OOAD_Project_Engine.Presentation;

namespace OOAD_Project_Engine.Foundation
{
    class ResourceManager
    {
        static Dictionary<string, Texture2D> _textures;
        static Dictionary<string, SoundEffect> _sounds;
        static Dictionary<string, SpriteFont> _fonts;
        static ContentManager _content;
        //static GraphicsDevice _graphics;
        //public static string ContentRoot { get; set; }

        public static void Initialize(ContentManager content)
        {
            _textures = new Dictionary<string, Texture2D>();
            _sounds = new Dictionary<string, SoundEffect>();
            _fonts = new Dictionary<string, SpriteFont>();
            _content = content;
            //_graphics = graphics;
            //ContentRoot = "";
        }

        public static Texture2D LoadTexture2DFromFile(GraphicsDevice graphics,string fileName)
        {
            Texture2D texture;
            string path = fileName;
            if (_textures.ContainsKey(path))
                texture = _textures[path];
            else
            {
                FileStream fileStream = new FileStream(@path, FileMode.Open);
                texture = Texture2D.FromStream(graphics, fileStream);
                _textures.Add(path, texture);
            }
            return texture;
        }

        public static Texture2D LoadTexture2DFromContent(string textureName)
        {
            Texture2D texture;
            string path = textureName;
            if (_textures.ContainsKey(path))
                texture = _textures[path];
            else
            {
                texture = _content.Load<Texture2D>(path);
                _textures.Add(path, texture);
            }
            return texture;
        }

        public static SoundEffect LoadSoundEffectFromContent(string soundName)
        {
            SoundEffect sound;
            string path = soundName;
            if (_sounds.ContainsKey(path))
                sound = _sounds[path];
            else
            {
                sound = _content.Load<SoundEffect>(path);
                _sounds.Add(path, sound);
            }
            return sound;
        }

        public static SpriteFont LoadSpriteFontFromContent(string fontName)
        {
            SpriteFont font;
            string path = fontName;
            if (_fonts.ContainsKey(path))
                font = _fonts[path];
            else
            {
                font = _content.Load<SpriteFont>(path);
                _fonts.Add(path, font);
            }
            return font;
        }

        public static void Dispose()
        {
            foreach (Texture2D t in _textures.Values)
                t.Dispose();
            foreach (SoundEffect s in _sounds.Values)
                s.Dispose();
            _textures.Clear();
            _sounds.Clear();
        }
    }
}
